<?php

// 团购商品表

namespace app\console\model;

use think\Db;
use think\Model;

class GroupGoods extends Model
{
    // 团购商品 商品详情关联
    public function ShopGoods()
    {
        return $this->hasOne('ShopGoods','id','goods_id');
    }


    // 团购商品 店铺关联
    public function Shop()
    {
        return $this->belongsTo('Shop','shop_id','id');
    }


    // 模型事件
    protected static function init()
    {

        // 写入之前
        self::event('before_write',function ($data) {

            // 店铺ID
            $shopId = Db::name('ShopGoods')->where('id',$data->goods_id)->find();
            $data->shop_id = $shopId['member_id'];

            // 商品名
            $data->title = $shopId['title'];
        });

        // 写入之后
        self::event('after_write',function ($data) {

            // 更新商品信息
            Db::name('ShopGoods')->where('id',$data->goods_id)->update(['activity'=>1]);

        });

        // 删除之前
        self::event('before_delete',function ($data) {

            Db::name('ShopGoods')->where('id',$data->goods_id)->update(['activity'=>2]);


        });
    }



}